package net.zabuchy.dashboard.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.zabuchy.authenticationHelper.AuthenticationHelper;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.bo.IssueBo;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.iteration.bo.IterationBo;
import net.zabuchy.meeting.entity.Meeting;
import net.zabuchy.project.action.ProjectAction;
import net.zabuchy.project.bo.ProjectBo;
import net.zabuchy.project.entity.Project;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class DashboardAction extends  ActionSupport {
	
	private static final Logger LOGGER = Logger.getLogger(ProjectAction.class.getName());
	
	@Autowired
    private SessionHelper sessionBean;
	
	@Autowired
	private AuthenticationHelper authenticationHelper;
	
	@Autowired
    private UserBo userBo;
	
	@Autowired
    private ProjectBo projectBo;
	
	@Autowired
	private IterationBo iterationBo;
	
	@Autowired
	private IssueBo issueBo;
	
	
	private User user;
	private Set<Project> projects = new HashSet<Project>(0);
	private Set<Meeting> meetings = new HashSet<Meeting>(0);
	private Set<String> projectsNames = new HashSet<String>(0);
	private List<Issue> issuesInactualIteration;
	private String yourProject;
	
	private int issuesInActualIterationCount;
	private int issueAssignedToUserCount;
	
	public String listProjectsForUser() {
    	
    	user = userBo.findByUsername(authenticationHelper.getName());
    	projects = user.getProjects();
    	LOGGER.debug("projects.size " + projects.size());
    	for(Project project : projects) {
    		projectsNames.add(project.getTitle());
    	}
    	LOGGER.debug("DashboardAction.listProjectsForUser projectNames.size() " + projectsNames.size());
    	
    	return Action.SUCCESS;
    }
	
	public String chooseProject() {
		
		Project currentProject = projectBo.findByTitle(yourProject);
		int currentProjectId = currentProject.getProjectId();
		
		//		put projectId into session
		sessionBean.setProjectId(currentProjectId);
		
		sessionBean.setActualIteration(iterationBo.findActualIteratio());
		
		LOGGER.debug("DashboadrAction.chooseProject()");
		LOGGER.debug("DashboadrAction.chooseProject() yourProject " + yourProject);
		return Action.SUCCESS;
	}
	

	public String prepareDashboard() {
		
		user = sessionBean.getExecutor();
		meetings = user.getMeetings();
		LOGGER.debug("meetings.size() " + meetings.size());
		
		issuesInactualIteration = issueBo.findIssuesRelatedToIteration(sessionBean.getActualIteration().getIterationId());
		issuesInActualIterationCount = issuesInactualIteration.size();
		LOGGER.debug("issuesInActualIterationCount:" + issuesInActualIterationCount);
		
		issueAssignedToUserCount = user.getIssues().size();
		LOGGER.debug("issueAssignedToUserCount:" + issueAssignedToUserCount);
		
		return Action.SUCCESS;
	}
	
	public String execute() {
		LOGGER.debug("DashboadrAction.execute()");
		return SUCCESS;
	}

	public void setProjects(Set projects) {
		this.projects = projects;
	}

	public Set getProjects() {
		return projects;
	}

	public void setProjectsNames(Set projectsNames) {
		this.projectsNames = projectsNames;
	}

	public Set getProjectsNames() {
		return projectsNames;
	}

	public void setYourProject(String yourProject) {
		this.yourProject = yourProject;
	}

	public String getYourProject() {
		return yourProject;
	}

	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public ProjectBo getProjectBo() {
		return projectBo;
	}

	public void setProjectBo(ProjectBo projectBo) {
		this.projectBo = projectBo;
	}

	public AuthenticationHelper getAuthenticationHelper() {
		return authenticationHelper;
	}

	public void setAuthenticationHelper(AuthenticationHelper authenticationHelper) {
		this.authenticationHelper = authenticationHelper;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setMeetings(Set<Meeting> meetings) {
		this.meetings = meetings;
	}

	public Set<Meeting> getMeetings() {
		return meetings;
	}

	/**
	 * @param issuesInactualIteration the issuesInactualIteration to set
	 */
	public void setIssuesInactualIteration(List<Issue> issuesInactualIteration) {
		this.issuesInactualIteration = issuesInactualIteration;
	}

	/**
	 * @return the issuesInactualIteration
	 */
	public List<Issue> getIssuesInactualIteration() {
		return issuesInactualIteration;
	}

	public IssueBo getIssueBo() {
		return issueBo;
	}

	public void setIssueBo(IssueBo issueBo) {
		this.issueBo = issueBo;
	}

	/**
	 * @param issuesInActualIterationCount the issuesInActualIterationCount to set
	 */
	public void setIssuesInActualIterationCount(int issuesInActualIterationCount) {
		this.issuesInActualIterationCount = issuesInActualIterationCount;
	}

	/**
	 * @return the issuesInActualIterationCount
	 */
	public int getIssuesInActualIterationCount() {
		return issuesInActualIterationCount;
	}

	/**
	 * @param issueAssignedToUserCount the issueAssignedToUserCount to set
	 */
	public void setIssueAssignedToUserCount(int issueAssignedToUserCount) {
		this.issueAssignedToUserCount = issueAssignedToUserCount;
	}

	/**
	 * @return the issueAssignedToUserCount
	 */
	public int getIssueAssignedToUserCount() {
		return issueAssignedToUserCount;
	}

	/**
	 * @param iterationBo the iterationBo to set
	 */
	public void setIterationBo(IterationBo iterationBo) {
		this.iterationBo = iterationBo;
	}

	/**
	 * @return the iterationBo
	 */
	public IterationBo getIterationBo() {
		return iterationBo;
	}
	
}